﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Data.Common;
using NetAcademia.Web.Mvc;
using System.Drawing.Imaging;
using System.IO;
using NetAcademia.Configuration;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;
using System.Collections.Specialized;
using System.Web;
using System.Web.Mvc;

namespace NetAcademia.Web.Mvc.ImageRendering
{
    public static class SqlImageHelper
    {
        #region img
        public static HtmlString SqlImage(this HtmlHelper html, string key, object sqlParameters)
        {
            return SqlImage(html, null, null, key, null, null, null, sqlParameters, null);
        }

        public static HtmlString SqlImage(this HtmlHelper html, string key, string text, string image)
        {
            return SqlImage(html, null, null, key, text, image, null, null, null);
        }

        public static HtmlString SqlImage(this HtmlHelper html, string key, string image, object sqlParameters, object htmlAttributes)
        {
            return SqlImage(html, null, null, key, null, image, null, sqlParameters, htmlAttributes);
        }

        public static HtmlString SqlImage(this HtmlHelper html, string key, string image, double rotation, object sqlParameters, object htmlAttributes)
        {
            return SqlImage(html, null, null, key, null, image, rotation, sqlParameters, htmlAttributes);
        }

        public static HtmlString SqlImage(this HtmlHelper html, string key, string text, string image, double? rotation, object sqlParameters, object htmlAttributes)
        {
            return SqlImage(html, null, null, key, text, image, rotation, sqlParameters, htmlAttributes);
        }

        public static HtmlString SqlImage(this HtmlHelper html, string controller, string action, string key, string text, string image, double? rotation, object sqlParameters, object htmlAttributes)
        {
            return SqlImageImgHtml(html, controller, action, key, text, image, rotation, sqlParameters, htmlAttributes, RenderingTags.img);
        }
        #endregion


        #region linkedimage
        public static HtmlString SqlImageLinked(this HtmlHelper html, string key, object sqlParameters, object linkRouteValues)
        {
            UrlHelper url = new UrlHelper(html.ViewContext.RequestContext,html.RouteCollection);
            string linkUrl = url.RouteUrl(linkRouteValues);
            return SqlImageLinked(html, null, null, key, null, null, null, sqlParameters, null, linkUrl);
        }

        public static HtmlString SqlImageLinked(this HtmlHelper html, string key, object sqlParameters, string linkUrl)
        {
            return SqlImageLinked(html, null, null, key, null, null, null, sqlParameters, null, linkUrl);
        }

        public static HtmlString SqlImageLinked(this HtmlHelper html, string key, string text, string image,  object linkRouteValues)
        {
            UrlHelper url = new UrlHelper(html.ViewContext.RequestContext, html.RouteCollection);
            string linkUrl = url.RouteUrl(linkRouteValues);
            return SqlImageLinked(html, null, null, key, text, image, null, null, null, linkUrl);
        }

        public static HtmlString SqlImageLinked(this HtmlHelper html, string key, string text, string image, string linkUrl)
        {
            return SqlImageLinked(html, null, null, key, text, image, null, null, null, linkUrl);
        }
 
        public static HtmlString SqlImageLinked(this HtmlHelper html, string key, string image, object sqlParameters, object htmlAttributes, object linkRouteValues)
        {
            UrlHelper url = new UrlHelper(html.ViewContext.RequestContext, html.RouteCollection);
            string linkUrl = url.RouteUrl(linkRouteValues);
            return SqlImageLinked(html, null, null, key, null, image, null, sqlParameters, htmlAttributes, linkUrl);
        }

        public static HtmlString SqlImageLinked(this HtmlHelper html, string key, string image, object sqlParameters, object htmlAttributes, string linkUrl)
        {
            return SqlImageLinked(html, null, null, key, null, image, null, sqlParameters, htmlAttributes, linkUrl);
        }

        public static HtmlString SqlImageLinked(this HtmlHelper html, string key, string image, double rotation, object sqlParameters, object htmlAttributes, object linkRouteValues)
        {
            UrlHelper url = new UrlHelper(html.ViewContext.RequestContext, html.RouteCollection);
            string linkUrl = url.RouteUrl(linkRouteValues);
            return SqlImageLinked(html, null, null, key, null, image, rotation, sqlParameters, htmlAttributes, linkUrl);
        }

        public static HtmlString SqlImageLinked(this HtmlHelper html, string key, string image, double rotation, object sqlParameters, object htmlAttributes, string linkUrl)
        {
            return SqlImageLinked(html, null, null, key, null, image, rotation, sqlParameters, htmlAttributes, linkUrl);
        }

        public static HtmlString SqlImageLinked(this HtmlHelper html, string key, string text, string image, double? rotation, object sqlParameters, object htmlAttributes, object linkRouteValues)
        {
            UrlHelper url = new UrlHelper(html.ViewContext.RequestContext, html.RouteCollection);
            string linkUrl = url.RouteUrl(linkRouteValues);
            return SqlImageLinked(html, null, null, key, text, image, rotation, sqlParameters, htmlAttributes, linkUrl);
        }

        public static HtmlString SqlImageLinked(this HtmlHelper html, string key, string text, string image, double? rotation, object sqlParameters, object htmlAttributes, string linkUrl)
        {
            return SqlImageLinked(html, null, null, key, text, image, rotation, sqlParameters, htmlAttributes, linkUrl);
        }

        public static HtmlString SqlImageLinked(this HtmlHelper html, string controller, string action, string key, string text, string image, double? rotation, object sqlParameters, object htmlAttributes, object linkRouteValues)
        {
            UrlHelper url = new UrlHelper(html.ViewContext.RequestContext, html.RouteCollection);
            string linkUrl = url.RouteUrl(linkRouteValues);
            return SqlImageImgHtml(html, controller, action, key, text, image, rotation, sqlParameters, htmlAttributes, RenderingTags.a, linkUrl);
        }

        public static HtmlString SqlImageLinked(this HtmlHelper html, string controller, string action, string key, string text, string image, double? rotation, object sqlParameters, object htmlAttributes, string linkUrl)
        {
            return SqlImageImgHtml(html, controller, action, key, text, image, rotation, sqlParameters, htmlAttributes, RenderingTags.a, linkUrl);
        }
        #endregion


        #region input
        public static HtmlString SqlImageInput(this HtmlHelper html, string key, object sqlParameters)
        {
            return SqlImageInput(html, null, null, key, null, null, null, sqlParameters, null);
        }

        public static HtmlString SqlImageInput(this HtmlHelper html, string key, string text, string image)
        {
            return SqlImageInput(html, null, null, key, text, image, null, null, null);
        }

        public static HtmlString SqlImageInput(this HtmlHelper html, string key, string image, object sqlParameters, object htmlAttributes)
        {
            return SqlImageInput(html, null, null, key, null, image, null, sqlParameters, htmlAttributes);
        }

        public static HtmlString SqlImageInput(this HtmlHelper html, string key, string image, double rotation, object sqlParameters, object htmlAttributes)
        {
            return SqlImageInput(html, null, null, key, null, image, rotation, sqlParameters, htmlAttributes);
        }

        public static HtmlString SqlImageInput(this HtmlHelper html, string key, string text, string image, double? rotation, object sqlParameters, object htmlAttributes)
        {
            return SqlImageInput(html, null, null, key, text, image, rotation, sqlParameters, htmlAttributes);
        }

        public static HtmlString SqlImageInput(this HtmlHelper html, string controller, string action, string key, string text, string image, double? rotation, object sqlParameters, object htmlAttributes)
        {
            return SqlImageImgHtml(html, controller, action, key, text, image, rotation, sqlParameters, htmlAttributes, RenderingTags.input);
        }
        #endregion

        #region url
        public static HtmlString SqlImageUrl(this UrlHelper url, string key, object sqlParameters)
        {
            return SqlImageUrl(url, null, null, key, null, null, null, sqlParameters);
        }

        public static HtmlString SqlImageUrl(this UrlHelper url, string key, string image, object sqlParameters)
        {
            return SqlImageUrl(url, null, null, key, null, image, null, sqlParameters);
        }

        public static HtmlString SqlImageUrl(this UrlHelper url, string key,  string image, double rotation, object sqlParameters)
        {
            return SqlImageUrl(url, null, null, key, null, image, rotation, sqlParameters);
        }

        public static HtmlString SqlImageUrl(this UrlHelper url, string key, string text, string image, double? rotation, object sqlParameters)
        {
            return SqlImageUrl(url, null, null, key, text, image, rotation, sqlParameters);
        }


        public static HtmlString SqlImageUrl(this UrlHelper url, string controller, string action, string key, string text, string image, double? rotation, object sqlParameters)
        {
            return new HtmlString(GetSqlImageUrl(url, controller, action, key, text, image, rotation, sqlParameters));
        }
        #endregion

        private static HtmlString SqlImageImgHtml(HtmlHelper html, string controller, string action, string key, string text, string image, double? rotation, object sqlParameters, object htmlAttributes, RenderingTags tag, string linkUrl = null)
        {
            UrlHelper url = new UrlHelper(html.ViewContext.RequestContext, html.RouteCollection);
            string imageUrl = GetSqlImageUrl(url, controller, action, key, text, image, rotation, sqlParameters);

            return XamlToPngHelper.GenerateHtml(imageUrl, htmlAttributes, tag, anchorUrl: linkUrl);
        }

        private static string  GetSqlImageUrl(this UrlHelper url, string controller, string action, string key, string text, string image, double? rotation, object sqlParameters)
        {
            if (string.IsNullOrWhiteSpace(action))
            {
                controller = "XAMLtoMVC";
                action = "SqlImage";
            }


            string imageUrl = url.RouteUrl(new { controller = controller, action = action, id = key, text = text, image = image, rotation = rotation });

            if (sqlParameters != null)
            {
                var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(sqlParameters);
                StringBuilder sb = new StringBuilder(attributes.Count * 30);
                
                foreach (var item in attributes)
                {
                    sb.AppendFormat("&_{0}={1}", HttpUtility.UrlEncode(item.Key), HttpUtility.UrlEncode(string.Format("{0}", item.Value)));
                }

                if (!imageUrl.Contains('?')) //már van valami a querystring-ben
                {
                    sb.Remove(0, 1);
                    sb.Insert(0, '?');
                }
                imageUrl += sb.ToString();
            }
            return imageUrl;
        }
    }
}